package br.com.wssa.smmb.controller;

import java.math.BigDecimal;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import br.com.wssa.smmb.constants.SmmbConstants;
import br.com.wssa.smmb.dto.MensagemDTO;
import br.com.wssa.smmb.dto.SimulacaoDTO;
import br.com.wssa.smmb.entidades.Usuario;
import br.com.wssa.smmb.enums.StatusCodeEnum;
import br.com.wssa.smmb.enums.TipoUsuarioEnum;

/**
 * Classe SiteController.
 * @author ssilva
 */
@Controller
public class SiteController {

	/**
	 * A anotação @RequestMapping identifica qual a URL relacionada ao método (action)
	 * a ser executado.
	 * 
	 * Neste exemplo, vemos que a URL padrão para nosso sistema, o "/" sempre apontará para 
	 * esta chamada.
	 * @param model
	 * @return
	 */
	@RequestMapping("/")
	public String index(Map<String, Object> model, HttpSession sessao) {
		if (sessao.getAttribute("SPRING_SECURITY_CONTEXT") == null) {
			return "index";
		} else {
			return "index";
		}
	}

	/**
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/home", method = RequestMethod.POST,
		consumes = MediaType.APPLICATION_JSON_VALUE, produces = SmmbConstants.PRODUCES_JSON_VALUE)
    public ModelAndView home() {
		ModelAndView modelView = new ModelAndView("/index");
		return modelView;
	}
	
	/**
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/quemSomos", method = RequestMethod.POST,
		consumes = MediaType.APPLICATION_JSON_VALUE, produces = SmmbConstants.PRODUCES_JSON_VALUE)
    public ModelAndView quemSomos() {
		ModelAndView modelView = new ModelAndView("/quemSomos");
		return modelView;
	}

	/**
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/parceiros", method = RequestMethod.POST,
			consumes = MediaType.APPLICATION_JSON_VALUE, produces = SmmbConstants.PRODUCES_JSON_VALUE)	
	public ModelAndView medicamentos() {
		ModelAndView modelView = new ModelAndView("/parceiros");
		return modelView;
	}



	/**
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/entrar", method = RequestMethod.POST,
			consumes = MediaType.APPLICATION_JSON_VALUE, produces = SmmbConstants.PRODUCES_JSON_VALUE)	
	public ModelAndView entrar(Map<String, Object> model) {
		model.put("j_username", StringUtils.EMPTY);
		model.put("j_password", StringUtils.EMPTY);
		model.put("loginInvalido", false);
		model.put("mensagem", new MensagemDTO());
		ModelAndView modelView = new ModelAndView("/entrar", model);
		return modelView;
	}

	/**
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/loginFalho", method = RequestMethod.GET)
	public String loginFalho() {
		return "loginFalho";
	}

	/**
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/loginFalho", method = RequestMethod.POST,
			consumes = MediaType.APPLICATION_JSON_VALUE, produces = SmmbConstants.PRODUCES_JSON_VALUE)
	public ModelAndView loginFalho(Map<String, Object> model) {
		ModelAndView modelView = new ModelAndView("/loginFalho");
		return modelView;
	}
	
	/**
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/simulado", method = RequestMethod.POST,
			consumes = MediaType.APPLICATION_JSON_VALUE, produces = SmmbConstants.PRODUCES_JSON_VALUE)
	public ModelAndView simulado(Map<String, Object> model) {
		model.put("simulacao", new SimulacaoDTO());
		ModelAndView modelView = new ModelAndView("/simulado");
		return modelView;
	}

	/**
	 * @param model
	 * @return
	 */
	@RequestMapping("/erro")
	public String erro(HttpServletRequest request) {
		Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
		return StatusCodeEnum.obterStatusCode(statusCode).getDescricao();
	}

	/**
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/registro", method = RequestMethod.POST)
	public ModelAndView registro(Map<String, Object> model) {
		Usuario usuario = new Usuario();
		model.put("usuario", usuario);
		model.put("j_username", StringUtils.EMPTY);
		model.put("j_password", StringUtils.EMPTY);
		model.put("registroInvalido", false);
		model.put("tipoUsuario", TipoUsuarioEnum.PESSOA_FISICA);
		model.put("mensagem", new MensagemDTO());
		ModelAndView modelView = new ModelAndView("/registro", model);
		return modelView;
	}
	
	
	/**
	 * A anotação @RequestMapping identifica qual a URL relacionada ao método (action)
	 * a ser executado.
	 * 
	 * Neste exemplo, vemos que a URL padrão para nosso sistema, o "/" sempre apontará para 
	 * esta chamada.
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/calculaValor", method = RequestMethod.POST)
	public String calculaValor(String distancia) {
		BigDecimal valorMinimo = new BigDecimal("22.00"); 
		
		return valorMinimo.toString().replace(".", ",");
		
	}
	
	/**
	 * A anotação @RequestMapping identifica qual a URL relacionada ao método (action)
	 * a ser executado.
	 * 
	 * Neste exemplo, vemos que a URL padrão para nosso sistema, o "/" sempre apontará para 
	 * esta chamada.
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/solicitarEntrega", method = RequestMethod.POST)
	public ModelAndView solicitarEntrega(Map<String, Object> model, HttpSession sessao) {
		if (sessao.getAttribute("SPRING_SECURITY_CONTEXT") == null) {
			model.put("j_username", StringUtils.EMPTY);
			model.put("j_password", StringUtils.EMPTY);
			model.put("loginInvalido", false);
			model.put("mensagem", new MensagemDTO());
			ModelAndView modelView = new ModelAndView("/entrar", model);
			return modelView;
		} else {
			return null;
		}
	}
}